<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/19
 * Time: 16:28
 */

namespace app\api\controller;


use think\Db;

class Store extends Base
{
    public $user_id;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 门店列表
     * @return \think\response\Json
     * sheng      省ID
     * shi        市ID
     * qu         区ID
     * keyword    关键字
     */
    public function storeList(){
        $sheng = input('sheng');
        $shi = input('shi');
        $qu = input('qu');
        $keyword = input('keyword');
//        $lon = input('lon'); // 经度
//        $lat = input('lat'); // 纬度
        
        $where['s_verify'] = 2;
        $where['s_status'] = 2;
        if(isset($sheng) && $sheng != ''){
            $where['s_sheng'] = $sheng;
        }
        if(isset($shi) && $shi != ''){
            $where['s_shi'] = $shi;
        }
        if(isset($qu) && $qu != ''){
            $where['s_qu'] = $qu;
        }
        if(isset($keyword) && $keyword != ''){
            $where['s_com_name'] = ['like', '%' . $keyword . '%'];
        }
        // 没有经纬度，用查询条件去查询数据
        $field = 's_id, s_com_name, s_lon, s_lat';
        $store = Db::table('up_shop')->where($where)->field($field)->select();
        
        // 如果有经纬度，则根据经纬度获取10公里范围内的数据
//        if(isset($lon) && $lon != '' && isset($lat) && $lat != ''){
//            $field .= ',ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(('.$lat.'* PI() / 180 - store_lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(store_lat * PI() / 180) * POW(SIN(('.$lon.' * PI() / 180 - store_lon * PI() / 180) / 2),2))) * 1000) AS distance';
//            $store = Db::table('up_store')
//                ->where($where)
//                ->having('distance <= 10000')
//                ->field($field)
//                ->order('distance asc')
//                ->select();
//        }
        if($store){
            return json(['status' => 200, 'msg' => $store]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 店铺详情
     * @param $store_id   店铺ID
     * @return \think\response\Json
     */
    public function storeInfo($store_id){
        $store = Db::table('up_shop')
            ->where('s_id', $store_id)
            ->field('s_com_name, s_phone, s_phone1, s_phone2, s_area_str, s_address')
            ->find();
        $store['s_address'] = $store['s_area_str'] . $store['s_address'];
        $phone = '';
        if($store['s_phone']) $phone = $store['s_phone'];
        if($store['s_phone1']) $phone = $phone . ','. $store['s_phone1'];
        if($store['s_phone2']) $phone = $phone . ',' . $store['s_phone2'];
        $store['phone'] = trim($phone, ',');
        return json(['status' => 200, 'msg' => $store]);
    }
    
    /**
     * 获取省列表
     * @return \think\response\Json
     */
    public function getProvince(){
        $province = Db::table('up_area')
            ->where('level', 1)
            ->field('district_id, district')
            ->select();
        return json(['status' => 200, 'msg' => $province]);
    }
    
    /**
     * 根绝父级ID，获取下级
     * @param $district_id    父级的ID
     * @return \think\response\Json
     */
    public function getDistrictByPid($district_id){
        $district = Db::table('up_area')
            ->where('pid', $district_id)
            ->field('district_id, district')
            ->select();
        return json(['status' => 200, 'msg' => $district]);
    }
}